#include <stdio.h>
#include <stdlib.h>
#include "SeqQueue.h"

//初始化队列
queue_head *Init_Queue()
{
	queue_head *ptr = NULL;
	ptr = malloc(sizeof(*ptr));
	if(ptr == NULL)
		return NULL;
	ptr->front = NULL;
	ptr->rear = NULL;
	ptr->number = 0;
	return ptr;
}

//销毁队列
void Destroy_Queue(queue_head **ptr)
{
	Clear_Queue(*ptr);
	free(*ptr);
	*ptr = NULL;
}

//队列清空
void Clear_Queue(queue_head *ptr)
{
	if(ptr == NULL)
		return ;
	node_st *p = ptr->front;
	node_st *q = NULL;
	while(p)
	{
		q = p->next;
		free(p);
		p = q;
	}
}
//判断队列是否为空
int IsEmpty_Queue(queue_head *ptr)
{
	if(ptr == NULL || ptr->number == 0)
		return 1;
	return 0;

}

//队列的长度
int Length_Queue(queue_head *ptr)
{
	if(ptr == NULL)
		return 0;
	return ptr->number;
}

//出队列
int DeQueue(queue_head *ptr,Datatype *head_data)
{
	if(IsEmpty_Queue(ptr) || ptr->number == 0)
		return -1;
	*head_data = ptr->front->data;
	node_st *p = NULL;
	if(ptr->front->next)
	{
		p = ptr->front->next;
		free(ptr->front);
		ptr->front = p;
	}
	else
		ptr->front = NULL;
	ptr->number--;
}

//入队列
int EnQueue(queue_head *ptr,Datatype data)
{
	if(ptr == NULL)
		return -1;
	node_st *newnode = malloc(sizeof(*newnode));
	if(newnode == NULL)
		return -1;
	newnode->data = data;
	newnode->next = NULL;
	if(ptr->front == NULL)
	{
		ptr->front = newnode;
		ptr->rear = newnode;
	}

	else
	{
		ptr->rear->next = newnode;
		ptr->rear = newnode;
	
	}
	ptr->number++;
	return 0;
}

//获得队列头元素
int GetHead_Queue(queue_head *ptr,Datatype *head_data)
{
	if(ptr == NULL || ptr->number == 0)
		return -1;
	*head_data = ptr->front->data;
		return 0;
}
